package com.csizg.facerecognition;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
import com.csizg.facemodel.util.CompareResult;
import com.csizg.facemodel.view.ICompareListener;
import com.csizg.facemodel.view.MyHCameraView;
import java.io.File;
import org.opencv.android.CameraBridgeViewBase;

/**
 * 识别activity(横向)
 *
 * @author haohao(ronghao3508@gmail.com) on 2018/5/8 10:25
 * @version v1.0
 */
public class FdHActivity extends Activity {

    private static final String TAG = "OCVSample::Activity";

    private File mCascadeFile;
    private MyHCameraView mOpenCvCameraView;
    private TextView mSwitchBtn;
    private TextView mGetImage;
    private TextView mCompareImage;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.fdh_layout);

        mOpenCvCameraView = findViewById(R.id.fd_activity_surface_view);
        mOpenCvCameraView.setVisibility(CameraBridgeViewBase.VISIBLE);
        mSwitchBtn = findViewById(R.id.fd_activity_switch);
        mGetImage = findViewById(R.id.fd_activity_getImage);
        mCompareImage = findViewById(R.id.fd_activity_compare_image);

        initEvent();
    }

    private void initEvent() {
        mOpenCvCameraView.setICompareListener(new ICompareListener() {
            @Override
            public void onCompare(CompareResult result) {
                if (result == null) {
                    mCompareImage.setText("不是同一人");
                } else {
                    mCompareImage.setText("相似度" + result.getNum() + "%");
                }
            }

            @Override
            public void onCapture(boolean isTrue) {
                Toast.makeText(FdHActivity.this, isTrue ? "存储图片成功" : "存储图片失败", Toast.LENGTH_SHORT)
                        .show();
            }
        });
        mSwitchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOpenCvCameraView.switchCamera();
            }
        });
        mGetImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOpenCvCameraView.setCaptureFace();
            }
        });

        mCompareImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCompareImage.setText("识别中，请勿重复点击");
                mOpenCvCameraView.setCompareFace();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        init();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mOpenCvCameraView != null) mOpenCvCameraView.disableView();
    }

    private void init() {
        System.loadLibrary("opencv_java3");
        System.loadLibrary("detection_based_tracker_jni");
        mOpenCvCameraView.enableView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mOpenCvCameraView.disableView();
    }
}
